﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TuxedoLib;

namespace SpaceSquirrel
{
	class Program
	{
		private static readonly string[] ACTIONS = "left up right down A B start".Split(' ');

		private static GameSceneBase activeScene = null;
		private static int renderCounter = 0;
		private static Dictionary<string, bool> pressedActions;
		private static string saveScreenAs = null;
		private static Image virtualScreen = null;

		[STAThread]
		private static void Main(string[] args)
		{
			Constants.GO_TO_TITLE = false;
			TuxedoApp game = new TuxedoApp("Space Squirrel", 256 * 3, 224 * 3, true);
			game.StartLoad += Program.game_StartLoad;
			game.PostInit += Program.game_PostInit;
			game.Update += Program.game_Update;
			game.Render += Program.game_Render;

			game.StartPipeline(60);
		}

		private static void game_StartLoad(object sender, EventArgs e)
		{

		}

		static void game_PostInit(object sender, EventArgs e)
		{
			Image imageTest = new Image(2, 2);
			imageTest.Fill(255, 255, 255, 255);
			Draw.RectangleFill(imageTest, 5, 0, 0, 1, 1);
			Transform.FlipHorizontal(imageTest);

			Program.virtualScreen = new Image(256, 224);
			Program.pressedActions = new Dictionary<string, bool>();
			foreach (string action in ACTIONS)
			{
				Program.pressedActions[action] = false;
			}

			Program.activeScene = new OpeningScene();

			JoystickHelper.ConfigureJoysticksFromFile();
			JoystickHelper.GetJoysticksPresent();
			if (JoystickHelper.JoysticksPresent.ContainsKey(JoystickHelper.PreviousActiveJoystick))
			{
				JoystickHelper.SetActiveJoystick(JoystickHelper.PreviousActiveJoystick);
			}
		}

		private static void game_Update(object sender, UpdateEventArgs e)
		{
			List<MyEvent> events = new List<MyEvent>();

			JoystickHelper.PollActiveJoystick(events, Program.pressedActions);

			bool quitAttempt = false;
			PressedKeysLookup pressedKeys = e.PressedKeys;
			pressedActions["A"] = pressedKeys[Key.Space];
			pressedActions["B"] = pressedKeys[Key.B];
			pressedActions["left"] = pressedKeys[Key.Left];
			pressedActions["right"] = pressedKeys[Key.Right];
			pressedActions["up"] = pressedKeys[Key.Up];
			pressedActions["down"] = pressedKeys[Key.Down];
			pressedActions["start"] = pressedKeys[Key.Enter];

			foreach (Event ev in e.Events)
			{
				if (ev.Type == EventType.Key && ev.IsPress && ev.Key == Key.F4 && (pressedKeys[Key.LeftAlt] || pressedKeys[Key.RightAlt]))
				{
					quitAttempt = true;
				}
				
				if (ev.Type == EventType.Quit)
				{
					quitAttempt = true;
				}

				if (ev.Type == EventType.Key)
				{
					if (ev.Key == Key.Enter)
					{
						events.Add(new MyEvent("start", ev.IsPress));
					}
					else if (ev.Key == Key.Left)
					{
						events.Add(new MyEvent("left", ev.IsPress));
					}
					else if (ev.Key == Key.Right)
					{
						events.Add(new MyEvent("right", ev.IsPress));
					}
					else if (ev.Key == Key.Down)
					{
						events.Add(new MyEvent("down", ev.IsPress));
					}
					else if (ev.Key == Key.Up)
					{
						events.Add(new MyEvent("up", ev.IsPress));
					}
					else if (ev.Key == Key.Space)
					{
						events.Add(new MyEvent("A", ev.IsPress));
					}
					else if (ev.Key == Key.B)
					{
						events.Add(new MyEvent("B", ev.IsPress));
					}
					else if (ev.Key == Key.F12 && ev.IsPress)
					{
						int filename = 1;
						while (System.IO.File.Exists("screencap_" + filename + ".jpg"))
						{
							filename++;
						}
						Program.saveScreenAs = "screencap_" + filename + ".jpg";
					}
				}
			}

			if (quitAttempt)
			{
				Program.activeScene = null;
			}

			if (Program.activeScene != null)
			{
				Program.activeScene.ProcessInput(events, pressedActions);
				Program.activeScene.Update();
			}
		}

		private static void game_Render(object sender, RenderEventArgs e)
		{
			if (Program.activeScene != null)
			{
				bool largeScreen = Program.activeScene.Flags.Contains('W');

				if (largeScreen)
				{
					Program.activeScene.Render(e.Screen, Program.renderCounter);
				}
				else
				{
					Program.activeScene.Render(Program.virtualScreen, Program.renderCounter);
					Transform.Scale(Program.virtualScreen, e.Screen);
				}
				++Program.renderCounter;
				Program.activeScene = Program.activeScene.Next;
			}

		}
	}
}
